<?php
/**
 * Created by PhpStorm.
 * User: zy
 * Date: 2016/2/24
 * Time: 9:57
 */

namespace backend\models;

use linslin\yii2\curl\Curl;
use Yii;
use yii\web\UploadedFile;


class OpenapiShop extends \yii\base\Model
{
    public $bp_name; //名称
    public $bp_cover; // 封面图片
    public $bp_province; // 省
    public $bp_city;  // 市
    public $bp_district; // 县
    public $bp_tel; // 电话
    public $bp_is_sell; // 是否销售
    public $bp_medal_refund; // 勋章
    public $low_price; // 起步价
    public $open_year; // 开业年限
    public $shop_area; // 店铺面积
    public $service_num; // 项目
    public $facilities; // 设备设施
    public $business_time; // 营业时间
    public $tag; // 标签
    public $bp_photos; // 组图
    public $bp_owner_mobile; // 店长电话
    public $bp_intro; // 介绍


    public function rules()
    {
        return [
            [['bp_name'], 'required'],
            [['bp_province', 'bp_city', 'bp_district', 'bp_is_sell', 'business_time', 'tag', 'bp_owner_mobile', 'bp_intro', 'bp_tel', 'bp_medal_refund', 'low_price', 'open_year', 'shop_area', 'shop_area', 'service_num', 'facilities'], 'safe'],
//            [['bp_cover'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
//            [['bp_photos'], 'file', 'skipOnEmpty' => true, 'extensions' => 'jpg, png, gif, jpeg', 'maxFiles' => 6],
        ];
    }

    public function attributeLabels()
    {
        return [
            'bp_name' => '名称',
            'bp_address' => '地址',
            'bp_cover' => '封面图片',
//            'bp_photos' => '轮播图片',
            'bp_owner_mobile' => '店长电话',
            'bp_intro' => '描述'
        ];
    }

    /**
     * 单文件上传
     * @param $model
     * @param string $img
     * @return bool|string
     */
    public function uplaod($img = 'fileName')
    {
        $getFile = UploadedFile::getInstance(new self, $img);
        $path = './uploads/' . time() . '.' . $getFile->extension;
        $return = $getFile->saveAs($path);
        if ($return) {
            return $path;
        }
        return false;
    }

    /**
     * 上传多文件
     * @param string $img
     * @return array
     */
    public function uplaods($img = 'fileName')
    {
        $getFiles = UploadedFile::getInstances(new self, $img);
        $return = array();
        foreach ($getFiles as $file) {
            $path = './uploads/shop/' . time() . mt_rand(1000, 40000) . '.' . $file->extension;
            $file->saveAs($path);
            $return[] = $path;
        }
        return $return;
    }

    /**
     * 加密函数
     * @param $token
     * @param $nonce
     * @return string
     */
    public function create_sign($timestamp, $nonce)
    {
        $token = empty($token) ? 'thebossawageincrease' : $token;
        $signatureArray = array($token, $timestamp, $nonce);
        return sha1(implode($signatureArray));
    }

    /**
     * post发送请求
     * @param $timestamp
     * @param $nonce
     * @param $create_sign
     * @param $data
     * @param $url
     * @return mixed
     */
    public function get_url_msg($data, $url)
    {
        $timestamp = time();
        $nonce = rand(10000, 900000);
        $create_sign = $this->create_sign($timestamp, $nonce); // 生成签名
        $curl = new Curl();
        $response = $curl->setOption(
            CURLOPT_POSTFIELDS,
            http_build_query(array(
                    'timestamp' => $timestamp,
                    'nonce' => $nonce,
                    'sign' => $create_sign,
                    'data' => $data
                )
            ))
            ->post($url);
        return $response;
    }

}